$(document).ready(function(){
	menuActionPages();
});

function menuActionPages(){
	jQuery('li.view-pages a.function').click(function(){loadPages();});
	jQuery('li.add-pages a.function').click(function(){loadAddPage();});
}

function loadPages(){
	clearAdminPanels();
	loadTreepages(0);
	loadHeadProperties();
	loadBlocks();
}

function loadHeadProperties(){
	jQuery.ajax({
		type: "POST",
		url: "/ajax/core/admin/pages/loadProperties.php",
		data: {"idPage": idPage},
		success:function (response) {
			var json = eval(response);
			if (json.success) {
				appendHtml(json.html, jQuery('div.admin-col-l'));
				actionsModMetaTags();
				windowOpenClose();
			} else {
				alert(json.error);
			}
		}
	});
}

function loadBlocks(){
	//$('#admin-top-loader').show();
	toggleLoader(jQuery('div.admin-col-l'));
	
	jQuery.ajax({
		type: "POST",
		url: "/ajax/core/admin/pages/loadBlocks.php",
		data: {"idPage": idPage},
		success:function (response) {
			//$('#admin-top-loader').hide();
			toggleLoader(jQuery('div.admin-col-l'));
			var json = eval(response);
			if (json.success) {
				appendHtml(json.html, jQuery('div.admin-col-l'));
			} else {
				alert(json.error);
			}
		}
	});
}

function loadAddPage(){
	$('#admin-top-loader').show();
	
	jQuery.ajax({
		type: "POST",
		url: "/ajax/core/admin/pages/addPage.php",
		data: {"idPage": idPage},
		success:function (response) {
			$('#admin-top-loader').hide();
			var json = eval(response);
			if (json.success) {
				putHtml(json.html, jQuery('div.admin-col-l'));
				eventAjaxSendForm(jQuery('#admin-add-page'), '/ajax/core/admin/pages/createPage.php');
			} else {
				alert(json.error);
			}
		}
	});
}

/*
 * load action on li mod meta tags
 * */
function actionsModMetaTags(){
	var tagHtml = $('li.li-meta-tag').children('div').children();
	alert(tagHtml.parent().html());
	
	tagHtml.blur(function (){
		alert($(this).parent().html());
		//jQuery(this).unbind();
		//var field = jQuery(this).parent().parent().attr('class').split(' ');
		//proFieldModify(idProfessionista, field[0], jQuery(this), jQuery(this).val());		
	});
	
	tagHtml.keypress(function(e) {
		if (e.keyCode == '13') {
			$(this).blur();				
		}
	});
}

/*
 * load action on li for treePages
 * */
function actionsTreepages() {
	$('.admin-menu-r li span:not(.processed)').each(function (){
		$(this).addClass('processed');
		$(this).click(function (){
			loadSubTreepages($(this));
		});
	});
}

function viewSubTreePages(dom){
	if (dom.parent().children('ul').css('display') == 'none') {
		dom.parent().children('ul').show();
		dom.addClass('menu-arrow-down');
	} else {
		dom.parent().children('ul').hide();
		dom.removeClass('menu-arrow-down');
	}
}

function loadTreepages(parent){
	$('#admin-top-loader').show();
	
	jQuery.ajax({
		type: "POST",
		url: "/ajax/core/admin/pages/getTreePages.php",
		data: {"parent": parent, "labelPage": labelPage},
		success:function (response) {
			$('#admin-top-loader').hide();
			var json = eval(response);
			if (json.success) {
				appendHtml(json.html, jQuery('div.admin-col-r'));
				actionsTreepages();
			} else {
				alert(json.error);
			}
		}
	});
}

/*
 * load sub menu-r
 * */
function loadSubTreepages(dom){
	var idParentPage = dom.attr('class').replace('menu-page-', '');
		idParentPage = idParentPage.replace('menu-arrow ', '');
		
		$('#admin-top-loader').show();
		
		$.ajax({
		type:'POST',
		data: {"parent": idParentPage, "labelPage": labelPage},
		url:'/ajax/core/admin/pages/getTreePages.php',
		success:function (response) {
			$('#admin-top-loader').hide();
			var json = eval(response);
			if (json.success) {
				if (json.html != ''){
					appendHtml(json.html, dom.parent());
					dom.addClass('menu-arrow-down');
					actionsTreepages();
					dom.unbind('click');
					dom.click(function () {viewSubTreePages($(this));});
				} else {
					dom.unbind('click');
					dom.css('background-image', 'none');
				}
			} else {
				alert(json.error);
			}
			/*
			if (data != ''){
				dom.parent().append(data);
				dom.css('background-image', 'url(/img/ico/admin/arrow-down.png)');
				actionsTreepages();
				dom.unbind('click');
				dom.click(function () {viewSubTreePages($(this));});
			} else {
				dom.unbind('click');
				dom.css('background-image', 'none');
			}
			*/	
		}
	});
}